﻿using CcrFlows.Core;
using Microsoft.Ccr.Core;

namespace CcrFlows.Internal.Stages
{
    class JoinStage<TIn0, TIn1, TOut> : StageBase
    {
        internal JoinStage(JoinHandler<TIn0, TIn1, TOut> handler)
            : base(new IPort[] { new Port<TIn0>(), new Port<TIn1>() }, new[] { new Port<TOut>() })
        {
            base.InitializeInputHandler(Arbiter.JoinedReceive(
                                            false,
                                            (Port<TIn0>)base.inputPorts[0],
                                            (Port<TIn1>)base.inputPorts[1],
                                            (i0, i1) =>
                                                {
                                                    handler(i0, i1, (Port<TOut>)base.outputPorts[0]);
                                                    base.WaitForNextInput();
                                                }
                                            ));
        }
    }
}
